package com.manong.controller;


import com.manong.aspect.AutoFill;
import com.manong.entity.Permission;
import com.manong.service.PermissionService;
import com.manong.utils.Result;
import com.manong.vo.query.PermissionQueryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Dorr
 * @since 2025-03-03
 */
@RestController
@RequestMapping("/api/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    /**
     * 查询权限列表
     * @param permissionQueryVo
     * @return
     */
    @GetMapping("/list")
    public Result List(PermissionQueryVo permissionQueryVo){
        List<Permission> permissionList = permissionService.findPermissionList(permissionQueryVo);
        return Result.ok(permissionList);
    }

    /**
     * 查询父级权限列表
     * @return
     */
    @GetMapping("/parent/list")
    public Result parent(){
        List<Permission> parentPermissionList = permissionService.findParentPermissionList();
        return Result.ok(parentPermissionList);
    }

    /**
     * 根据ID查询菜单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getMenuById(@PathVariable Long id){
        Permission byId = permissionService.getById(id);
        return Result.ok(byId);
    }

    /**
     * 添加菜单和权限
     * @param permission
     * @return
     */
    @AutoFill(menu = "菜单管理", type = "添加")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:menu:add')")
    public Result add(@RequestBody Permission permission){
        if (permissionService.save(permission)) {
            return Result.ok().message("添加成功");
        }else{
            return Result.error().message("添加失败");
        }
    }

    /**
     * 修改菜单和权限
     * @param permission
     * @return
     */
    @AutoFill(menu = "菜单管理", type = "修改")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:menu:edit')")
    public Result update(@RequestBody Permission permission){
        if (permissionService.updateById(permission)) {
            return Result.ok().message("修改成功");
        }else {
            return Result.error().message("修改失败");
        }
    }

    /**
     * 删除菜单和权限
     * @param id
     * @return
     */
    @AutoFill(menu = "菜单管理", type = "删除")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result delete(@PathVariable Long id){
        if (permissionService.removeById(id)) {
            return Result.ok().message("删除成功");
        }else{
            return Result.error().message("删除失败");
        }
    }

    /**
     * 检查菜单下是否有子菜单
     * @param id
     * @return
     */
    @GetMapping("/check/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result check(@PathVariable Long id){
        //判断菜单是否有子菜单
        if(permissionService.hasChildrenOfPermission(id)){
            return Result.exist().message("该菜单下有子菜单，无法删除");
        }
        return Result.ok();
    }
}

